package com.github.catvod.spider;

import com.github.catvod.crawler.SpiderDebug;
import com.github.catvod.utils.Asset;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 适配FreeOK
 *
 * @Author zlliu
 * @Date 2025/8/28
 * @Description
 */
public class XPathFreeOK extends XPath {

    @Override
    protected String categoryUrl(String tid, String pg, boolean filter, HashMap<String, String> extend) {
        //  https://www.freeok.info/vod-show/{cateId}-{area}-{by}-{class}-----{catePg}---{year}.html
        //  extend: {key: cateId, value: ?sort=latest_release&page={catePg}}
        String area = "", by = "hits", classStr = "", year = "";
        if (extend != null && !extend.isEmpty()) {
            if (extend.containsKey("area")) {
                area = extend.get("area");
            }
            if (extend.containsKey("by")) {
                by = StringUtils.isNotBlank(extend.get("by")) ? extend.get("by") : by;
            }
            if (extend.containsKey("class")) {
                classStr = extend.get("class");
            }
            if (extend.containsKey("year")) {
                year = extend.get("year");
            }
        }
        return getCateUrl(true).replace("{cateId}", tid)
                .replace("{area}", area)
                .replace("{by}", by)
                .replace("{class}", classStr)
                .replace("{catePg}", pg)
                .replace("{year}", year)
                ;
    }

    /**
     * gitee json文件总提示违禁词，所以直接用本地的Filter配置
     *
     * @param json
     */
    @Override
    protected void loadRuleExt(String json) {
        try {
            String filter = Asset.read("freeok.json");
            this.rule.setFilter(new JSONObject(filter));
        } catch (Exception e) {
            SpiderDebug.log(e);
        }
    }
}